package anton.admin.schema;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "schemaBean")
@SessionScoped
public class Schema implements Serializable {

    private SimpleDateFormat sdf;
    private Calendar cal;
    private String mondayDate;
    private int week;
    private List<String> weekDates;
    private List<String> shift;
    private int weekToday;
    private String dateToday;

    public Schema() {
        week = 0;
        sdf = new SimpleDateFormat("yyyy-MM-dd");
        setMondayDate();
        setWeekDates();
        shift = new ArrayList<>();
        shift.add("Lunch");
        shift.add("Dinner");
    }

    private void setMondayDate() {
        cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.add(Calendar.DATE, week * 7);
        this.mondayDate = sdf.format(cal.getTime());
    }

    public int getWeek() {
        setMondayDate();
        setWeekDates();
        return cal.get(Calendar.WEEK_OF_YEAR);
    }

    public String getMondayDate() {
        return mondayDate;
    }

    public void nextWeek() {
        week += 1;
        setMondayDate();
        setWeekDates();
    }

    public void prevWeek() {
        week -= 1;
        setMondayDate();
        setWeekDates();
    }

    private void setWeekDates() {
        weekDates = new ArrayList<>();
        for (int d = 0; d < 7; d++) {
            try {
                cal.setTime(sdf.parse(mondayDate));
            } catch (ParseException ex) {
                Logger.getLogger(Schema.class.getName()).log(Level.SEVERE, null, ex);
            }
            cal.add(Calendar.DATE, d);
            weekDates.add(sdf.format(cal.getTime()));
        }
    }

    public List<String> getWeekDates() {
        return weekDates;
    }

    public List<String> getShift() {
        return shift;
    }

    public int getWeekToday() {
        cal = Calendar.getInstance();
        weekToday = cal.get(Calendar.WEEK_OF_YEAR);
        return weekToday;
    }

    public String getDateToday() {
        cal = Calendar.getInstance();
        dateToday = sdf.format(cal.getTime());
        return dateToday;
    }
    
    public String getShiftEmps(List<Shift> shiftList, String date, String time) {
        String emps = "";
        Iterator it = shiftList.iterator();
        while (it.hasNext()) {
            Shift temp = (Shift) it.next();
            if (date.equals(temp.getShift_date()) && time.equals(temp.getShift_time())) {
                emps += temp.getEmp_name() + "<br/>";
            }
        }
        if(emps.isEmpty())
            emps = "&nbsp;";
        return emps;
    }

    public String isSet(List<Shift> shiftList, String date, String time) {
        String shiftClass;
        int counter = 0;
        Iterator it = shiftList.iterator();
        while (it.hasNext()) {
            Shift temp = (Shift) it.next();
            if (date.equals(temp.getShift_date()) && time.equals(temp.getShift_time())) {
                counter++;
            }
        }
        if (counter != 0) {
            shiftClass = "work";
        } else {
            shiftClass = "nowork";
        }
        return shiftClass;
    }

    public List<String> getShiftEmpsUsername(List<Shift> shiftList, String date, String time) {
        List<String> emps = new ArrayList<>();
        Iterator it = shiftList.iterator();
        while (it.hasNext()) {
            Shift temp = (Shift) it.next();
            if (date.equals(temp.getShift_date()) && time.equals(temp.getShift_time())) {
                emps.add(temp.getUsername());
            }
        }
        return emps;

    }

    public int getShiftID(List<Shift> shiftList, String date, String time) {
        int id = -1;
        Iterator it = shiftList.iterator();
        while (it.hasNext()) {
            Shift temp = (Shift) it.next();
            if (date.equals(temp.getShift_date()) && time.equals(temp.getShift_time())) {
                id = temp.getShift_ID();
            }
        }
        return id;
    }
}
